package logic

import (
	"context"
	"errors"
	"house/model/mysql"
	"house/pkg"

	"house/house"
	"house/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserLogic {
	return &UpdateUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateUserLogic) UpdateUser(in *house.UpdateUserRequest) (*house.UpdateUserResponse, error) {
	// todo: add your logic here and delete this line
	var s mysql.Shop
	var u mysql.User
	s.UserId = int(in.UserId)
	if err := s.GetByUserId(); err != nil {
		logx.Error("房源查询失败")
		return nil, errors.New("房源查询失败")
	}
	if s.Title != "" {
		logx.Error("用户已经发布房源")
		return nil, errors.New("用户已经发布房源")
	}
	if u.Password == pkg.Md5(in.Newpassword) {
		logx.Error("新旧密码不能一样")
		return nil, errors.New("新旧密码不能一样")
	}
	u.Password = pkg.Md5(in.Newpassword)
	if err := u.Updates(); err != nil {
		logx.Error("修改失败")
		return nil, errors.New("修改失败")
	}
	
	return &house.UpdateUserResponse{
		Id: int64(u.ID),
	}, nil
}
